﻿using System;
using System.Collections.Generic;
using Facebook.Unity;
using GameAnalyticsSDK;
using UnityEngine;


namespace HWGames.Bundles.Internal.Analytics {
    internal static class AnalyticsManager {
        private const string TAG = "HWGamesAnalytics";

        internal static void Initialize(SDKBundleSettings Settings, bool consent) {
            FBMgr.Instance.Init();
            HWLog.Log("HWSDKBundle", "Initializing AnalyticsManager");
            SingularMgr.Instance.Init();
            HWLog.Log("HWSDKBundle", "Initializing Singular");
        }

        internal static void OnApplicationResume() {
            if (FBMgr.Instance.isInit) {
                FBMgr.Instance.Init();
            }
        }

        public static void InstantiateGameAnalytics() {
            var gameAnalyticsComponent = UnityEngine.Object.FindObjectOfType<GameAnalytics>();
            if (gameAnalyticsComponent == null) {
                var gameAnalyticsGameObject = new GameObject("GameAnalytics");
                gameAnalyticsGameObject.AddComponent<GameAnalytics>();
                gameAnalyticsGameObject.SetActive(true);
            }
            else {
                gameAnalyticsComponent.gameObject.name = "GameAnalytics";
            }

            GameAnalytics.Initialize();
            HWLog.Log("HWSDKBundle", "Initializing GameAnalytics");
        }

        private static void InitFBAdvertiserTracking() {
            if (FB.IsInitialized) {
                if (Tracking.TryIsAdvertisingTracking()) {
                    FB.Mobile.SetAdvertiserTrackingEnabled(true);
                }
            }
        }

        internal static void TrackGameStarted(string level) {
            GameAnalytics.NewProgressionEvent(GAProgressionStatus.Start, level);
        }

        internal static void TrackGameFinished(bool levelComplete, string level, int score = 0) {
            if (levelComplete) {
                GameAnalytics.NewProgressionEvent(GAProgressionStatus.Complete, level, score);
            }
            else {
                GameAnalytics.NewProgressionEvent(GAProgressionStatus.Fail, level, score);
            }
        }

        internal static void TrackCustomEvent(string eventName, Dictionary<string, object> eventValue) {
            string str = eventName;
            if (eventValue != null) {
                foreach (var item in eventValue) {
                    str = $"{eventName}:{item.Key}:{item.Value}";
                    GameAnalytics.NewDesignEvent(str);
                }
            }
            else {
                GameAnalytics.NewDesignEvent(str);
            }
            HWLog.Log(TAG, "Sending event " + str + " to GameAnalytics");
        }

        internal static void TrackCustomEvent(string eventName, params object[] eventValue) {
            string str = eventName;
            if (eventValue != null) {
                foreach (var item in eventValue) {
                    str += $":{item}";
                }
                GameAnalytics.NewDesignEvent(str);
            }
            else {
                GameAnalytics.NewDesignEvent(str);
            }
            HWLog.Log(TAG, "Sending event " + str + " to GameAnalytics");
        }

        internal static void TrackPurchaseEvent(string currency, int amount, string itemType, string itemId, string cartType) {
            if (!HWGames.Bundles.Internal.HWSDKBundleBehaviour.IsCheckKey) {
                return;
            }
            GameAnalyticsSDK.GameAnalytics.NewBusinessEvent(currency, amount, itemType, itemId, "shopwindow");
            HWLog.Log(TAG, "Sending purchase event " + itemType + " to GameAnalytics");
        }

        internal static void TrackFaceBookCustomEvent(string eventName, Dictionary<string, object> eventProperties) {
            FBMgr.Instance.Log_FB_Event(eventName, eventProperties);
        }

        //lite 未实现
        internal static void TrackFireBaseCustomEvent(string eventName, Dictionary<string, object> eventProperties) {
        }

        internal static void TrackSingularCustomEvent(string eventName, Dictionary<string, object> eventProperties = null) {
            if (eventProperties == null) {
                SingularMgr.Instance.LogEvent(eventName);
            }
            else {
                SingularMgr.Instance.LogEvent(eventName, eventProperties);
            }
        }
    }
}